<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.products_filter.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$xajax->registerFunction('getFilter');
$xajax->registerFunction('saveFilter');
$xajax->registerFunction('delFilter');
$xajax->registerFunction('saveNewFilter');
$xajax->registerFunction('deleteFilter');
$xajax->registerFunction('getParentDrop');
$xajax->registerFunction('saveCategories');
$xajax->registerFunction('getCategoryForm');

function getFilter($pID) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	$get_filter = $db->db_query("SELECT filter_id, filter_name FROM ".TABLE_PRODUCTS_FILTERS." WHERE parent_id = '0'");

	if($get_filter->_numOfRows) {
		$c = '<ul id="filter_form" style="list-style:none;margin:0;clear:both">';
		while(!$get_filter->EOF) {		
			$c .= '	<li id="filter_'.$get_filter->fields['filter_id'].'" style="border:1px solid #ccc;float:left;padding:1px 5px 1px 3px;margin:0 7px 7px 0">
						'.$get_filter->fields['filter_name'].' 
						<img onclick="if(confirm(\'Wollen Sie den Filter - '.$get_filter->fields['filter_name'].' - wirklich löschen?\n\nBeachten Sie das dieser Filter und seine Unterfilter auch aus den anderen Produkten gelöscht wird!\')){xajax_deleteFilter('.$get_filter->fields['filter_id'].', 1)}" src="images/icons/minus-circle.png" alt="" style="cursor:pointer;position:relative;top:3px" /> 
						<img title="Kategoriezuordnung ändern" alt="" onclick="xajax_getCategoryForm('.$get_filter->fields['filter_id'].')" style="cursor:pointer;position:relative;top:3px" src="images/icons/document-tree.png" />';
			$sub_filter = $db->db_query("SELECT 
											filter_name,
											filter_id 
										FROM 
											".TABLE_PRODUCTS_FILTERS." 
										WHERE 
											parent_id = '".$get_filter->fields['filter_id']."'");
			if($sub_filter->_numOfRows) {
				$c .= '<ul style="list-style:none;clear:both">';
				while(!$sub_filter->EOF) {
					$c .= '<li id="filter_'.$sub_filter->fields['filter_id'].'"> 
								<input id="ip_'.$sub_filter->fields['filter_id'].'" class="filter_input" type="checkbox" value="'.$get_filter->fields['filter_id'].'" name="'.$sub_filter->fields['filter_name'].'"'.checkFilter($pID, $sub_filter->fields['filter_id']).' /> 
								<label for="ip_'.$sub_filter->fields['filter_id'].'">'.$sub_filter->fields['filter_name'].'</label> 
								<img onclick="if(confirm(\'Wollen Sie den Filter - '.$sub_filter->fields['filter_name'].' - wirklich löschen?\n\nBeachten Sie das dieser Filter auch aus den anderen Produkten gelöscht wird!\')){xajax_deleteFilter('.$sub_filter->fields['filter_id'].')}" src="images/icons/minus-circle.png" alt="" style="cursor:pointer;position:relative;top:3px" />
							</li>';
					$sub_filter->MoveNext();
				}
				$c .= '</ul>';
			}
			$c .= '	</li>';
			$get_filter->MoveNext();
		}
		$c .= '</ul><br style="clear:both" />';
		$ajax->call('xajax_getParentDrop');
		return $ajax->assign('filter_ul', 'innerHTML', $c);
		
	} else 
		return $ajax->assign('filter_ul', 'innerHTML', '<i>Es wurden keine Filter in der Datenbank gefunden.</i>');
}

function checkFilter($pID, $fid) {
	global $db;
	
	$check = $db->db_query("SELECT 
								products_id 
							FROM 
								".TABLE_PRODUCTS_TO_FILTERS." 
							WHERE 
								filter_id = '".$fid."' 
							AND
								products_id = '".$pID."'");
	if($check->_numOfRows)
		return ' checked=""';
	else
		return false;
}

function saveFilter($fid, $status, $pID, $parent) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	$fid = explode('_', $fid);
	
	if($status == 1) {
		$sql_array = array('filter_id' => $fid[1], 'products_id' => $pID);
		$db->db_perform(TABLE_PRODUCTS_TO_FILTERS, $sql_array);
			
	} else {
		
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_TO_FILTERS." WHERE filter_id = '".$fid[1]."' AND products_id = '".$pID."'");
		
		$check = $db->db_query("SELECT filter_id FROM ".TABLE_PRODUCTS_TO_FILTERS." WHERE filter_id = '".$fid[1]."' ");
		if(!$check->_numOfRows) {
			$db->db_query("DELETE FROM ".TABLE_PRODUCTS_FILTERS." WHERE filter_id = '".$fid[1]."'");
			$j = '$("#filter_form #filter_'.$fid[1].'").fadeOut(600,function(){$(this).remove();});';
			$ajax->script($j);
			$ajax->call('xajax_getFilter', $pID);
		}
	}

	$j .= '$.gritter.add({'."\n";
	$j .= '		title: \'Erfolgreich gespeichert\','."\n";
	$j .= '		text: \'Der Filter wurde gespeichert.\','."\n";
	$j .= '		image: \''.DIR_WS_ICONS.'dialog-success.png\','."\n";
	$j .= '		sticky: false,'."\n";
	$j .= '		time: 3500'."\n";
	$j .= '});';
	
	$ajax->script($j);
	return $ajax;
}

function delFilter($pID) {
	global $db;
	$ajax = new xajaxResponse();
	
	$db->db_query("DELETE FROM ".TABLE_PRODUCTS_FILTERS." WHERE products_id = '".$pID."'");
	
	return $ajax;
}

function deleteFilter($id, $all = 0) {
	global $db;
	$ajax = new xajaxResponse();
	if($all == 1) {
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_FILTERS." WHERE filter_id = '".$id."'");
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_FILTERS." WHERE parent_id = '".$id."'");
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_TO_FILTERS." WHERE filter_id = '".$id."'");
	} else
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_FILTERS." WHERE filter_id = '".$id."'");
		
	$j .= '$.gritter.add({'."\n";
	$j .= '		title: \'Erfolgreich gespeichert\','."\n";
	$j .= '		text: \'Der Filter und alle Zuweisungen wurden gelöscht.\','."\n";
	$j .= '		image: \''.DIR_WS_ICONS.'dialog-success.png\','."\n";
	$j .= '		sticky: false,'."\n";
	$j .= '		time: 4500'."\n";
	$j .= '});';
	$j .= '$("#filter_form #filter_'.$id.'").fadeOut(600,function(){$(this).remove();});';
	
	$ajax->call('xajax_getParentDrop');
	return $ajax->script($j);
}

function saveNewFilter($val='', $parent_id='', $pID) {
	global $db;
	$ajax = new xajaxResponse();
	$check = $db->db_query("SELECT * FROM ".TABLE_PRODUCTS_FILTERS." WHERE filter_name = ".$db->db_prepare($val));
	
	if(empty($val)) {
		$j .= '$.gritter.add({'."\n";
		$j .= '		title: \'Hinweis\','."\n";
		$j .= '		text: \'Geben Sie einen gültigen Namen ein.\','."\n";
		$j .= '		image: \''.DIR_WS_ICONS.'dialog-warning.png\','."\n";
		$j .= '		sticky: false,'."\n";
		$j .= '		time: 3500'."\n";
		$j .= '});';
	
		return $ajax->script($j);
		
	} elseif($check->_numOfRows) {
		$j .= '$.gritter.add({'."\n";
		$j .= '		title: \'Fehler\','."\n";
		$j .= '		text: \'Dieser Filter ist bereits in der Datenbank vorhanden.\','."\n";
		$j .= '		image: \''.DIR_WS_ICONS.'dialog-error.png\','."\n";
		$j .= '		sticky: false,'."\n";
		$j .= '		time: 3500'."\n";
		$j .= '});';
	
		return $ajax->script($j);
		
	} else {
		if(!empty($parent_id) && $parent_id != 'undefined') {
			$db->db_query("INSERT INTO ".TABLE_PRODUCTS_FILTERS." (parent_id, filter_name) VALUES (".$db->db_prepare($parent_id).", ".$db->db_prepare($val).")");
			$db->db_query("INSERT INTO ".TABLE_PRODUCTS_TO_FILTERS." (filter_id, products_id) VALUES ('".$db->db_insert_id()."', ".$db->db_prepare($pID).")");
		} else
			$db->db_query("INSERT INTO ".TABLE_PRODUCTS_FILTERS." (filter_name, parent_id) VALUES (".$db->db_prepare($val).", '0')");
		
		$j .= '$.gritter.add({'."\n";
		$j .= '		title: \'Erfolgreich gespeichert\','."\n";
		$j .= '		text: \'Der Filter wurde gleich mit der Produkt-ID in der Datenbank gespeichert.\','."\n";
		$j .= '		image: \''.DIR_WS_ICONS.'dialog-success.png\','."\n";
		$j .= '		sticky: false,'."\n";
		$j .= '		time: 4500'."\n";
		$j .= '});'."\n";
		$j .= '$("#new_filter").val("");';
		
		$ajax->call('xajax_getFilter', $pID);
		
		if(empty($parent_id))
			$ajax->call('xajax_getCategoryForm', 0, 1, $db->db_insert_id());
		
		return $ajax->script($j);
	}
	
	return $ajax;
}

function getCategoryForm($id = '') {
	$ajax = new xajaxResponse();
	
	$cat_tree = getCategories(0, 1, $id);
		
	$f = '	<form id="cat_form" name="choose_cat" action="categories.php" method="post">
				<p>In welchen Kategorien soll der Filter erscheinen?</p>
				'.$cat_tree.'<br />
				<button onclick="xajax_saveCategories(xajax.getFormValues(\'cat_form\')); return false;" class="button">
					<span class="button_disk-black">Speichern</span>
				</button> 
				<button onclick="closeCategoryForm(); return false;" class="button">
					schliessen
				</button>
				<input type="hidden" name="filter_id" value="'.$id.'" />
			</form>';
	
	$ajax->assign('category_form', 'innerHTML', $f);
	$ajax->call('openCategoryForm');
	
	return $ajax;
}

function getParentDrop() {
	global $db;
	$ajax = new xajaxResponse();
	
	$parent = $db->db_query("SELECT filter_id, filter_name FROM ".TABLE_PRODUCTS_FILTERS." WHERE parent_id = '0'");
	
	if($parent->_numOfRows)	{
		$drop  = '<select name="parent_name" id="parent_name">';
		$drop .= '<option value="">-- unterordnen? --</option>';
		while(!$parent->EOF) {
			$drop .= '<option value="'.$parent->fields['filter_id'].'">'.$parent->fields['filter_name'].'</option>';
			$parent->MoveNext();
		}
		$drop .= '</select>';
		$ajax->assign('parent_filter_drop', 'innerHTML', $drop);
		$ajax->assign('parent_filter_drop', 'style.display', 'block');
	}
	return $ajax;
}

function saveCategories($val='') {
	global $db;
	$ajax = new xajaxResponse();
	#return $ajax->alert(print_r($val, true));
	if(!isset($val['cat_id'][0]))
		return $ajax->alert('Wählen Sie mindestens eine Kategorie aus.');
		
	else {
		$db->db_query("UPDATE 
							".TABLE_PRODUCTS_FILTERS." 
						SET 
							cat_id = ".$db->db_prepare(implode('|', $val['cat_id']))." 
						WHERE 
							filter_id = ".$db->db_prepare($val['filter_id'])." ");

		$j .= '$.gritter.add({'."\n";
		$j .= '		title: \'Erfolgreich gespeichert\','."\n";
		$j .= '		text: \'Die Kategoriezuordnung des Filters wurde gespeichert.\','."\n";
		$j .= '		image: \''.DIR_WS_ICONS.'dialog-success.png\','."\n";
		$j .= '		sticky: false,'."\n";
		$j .= '		time: 4500'."\n";
		$j .= '});'."\n";
		
		return $ajax->script($j);
	}
}

function getCategories($cat_id = 0, $level = 1, $filter = '') {
	global $db;
	$cats = $db->db_query("SELECT
								c.categories_id,
								cd.categories_name
							FROM
								".TABLE_CATEGORIES." c
								LEFT JOIN ".TABLE_CATEGORIES_DESCRIPTION." AS cd
								ON (cd.categories_id = c.categories_id AND cd.language_id = ".$db->db_prepare($_SESSION['languages_id']).")
							WHERE
								c.parent_id = ".$db->db_prepare($cat_id)."
							AND
								c.categories_status = '1'
							ORDER BY 
								sort_order");
	
	if($level == 1) {
		$check = $db->db_query("SELECT 
									cat_id 
								FROM 
									".TABLE_PRODUCTS_FILTERS." 
								WHERE 
									filter_id = '".(int)$filter."'");
		if($check->fields['cat_id'] == 0) {
			$all = ' checked=""';
			$hide = 'opacity:.3';
			$dis = ' disabled=""';
		}
		$cat_all = '<ul style="list-style:none"><li style="padding-bottom:5px;margin-bottom:5px;border-bottom:1px solid #ccc"><input name="cat_id[]"'.$all.' value="0" type="checkbox" class="click_all" /> immer anzeigen</li></ul>';
	}
	while(!$cats->EOF) {
		if(!empty($filter) && $filter != 0) {
			$checked = '';
			$check_cat = $db->db_query("SELECT 
											cat_id 
										FROM 
											".TABLE_PRODUCTS_FILTERS." 
										WHERE 
											filter_id = '".(int)$filter."'");
											
			if($check_cat->fields['cat_id'] != 0) {
				$array = explode('|', $check_cat->fields['cat_id']);
				if(in_array($cats->fields['categories_id'], $array))
					$checked = ' checked=""';
			}
		}
		
		$cat_tree .= '	<li>';
		$cat_tree .= '		<input name="cat_id[]"'.$checked.$dis.' type="checkbox" value="'.$cats->fields['categories_id'].'" /> ';
		$cat_tree .= '		'.$cats->fields['categories_name'];
		$cat_tree .= '		'.getCategories($cats->fields['categories_id'], $level+1, $filter);
		$cat_tree .= '	</li>';
		
		$cats->MoveNext();
	}
	if($cat_tree)
		return $cat_all.'<ul style="list-style:none;'.($level == 1 ? $hide :'').'"'.($level == 1 ? ' class="cat_tree"' :'').'>'.$cat_tree.'</ul>';
}
?>